2020-2021 Sunseeker Telemetry and Lighting System
sac_ex3_twoStageAmplifier.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
41 // -------------------
42 // /|\| OA0-|
43 // | | OA0O|
44 // | | OA0+|
45 // --|RST |
46 // | OA2-|
47 // | OA2O|
48 // | OA2+|<--
49 // | | |
50 // | ADC A1|
51 // | |
52 // | LED1|-->
57 //******************************************************************************
58 #include "Board.h"
59 #include "driverlib.h"
60 
61 uint16_t adcResult;
62 
63 int main(void)
64 {
65  //Stop WDT
66  WDT_A_hold(WDT_A_BASE);
67 
68  //Setup LED1
69  GPIO_setAsOutputPin(
70  GPIO_PORT_LED1,
71  GPIO_PIN_LED1);
72  //Set LED1 low
74  GPIO_PORT_LED1,
75  GPIO_PIN_LED1);
76 
77  /*
78  * Disable the GPIO power-on default high-impedance mode
79  * to activate previously configured port settings
80  */
81  PMM_unlockLPM5();
82 
83  //Configure OA0 and OA2 functionality
84  GPIO_setAsPeripheralModuleFunctionOutputPin(
85  GPIO_PORT_SACOA0O,
86  GPIO_PIN_SACOA0O,
87  GPIO_FUNCTION_SACOA0O);
88  GPIO_setAsPeripheralModuleFunctionInputPin(
89  GPIO_PORT_SACOA0N,
90  GPIO_PIN_SACOA0N,
91  GPIO_FUNCTION_SACOA0N);
92  GPIO_setAsPeripheralModuleFunctionInputPin(
93  GPIO_PORT_SACOA0P,
94  GPIO_PIN_SACOA0P,
95  GPIO_FUNCTION_SACOA0P);
96  GPIO_setAsPeripheralModuleFunctionOutputPin(
97  GPIO_PORT_SACOA2O,
98  GPIO_PIN_SACOA2O,
99  GPIO_FUNCTION_SACOA2O);
100  GPIO_setAsPeripheralModuleFunctionInputPin(
101  GPIO_PORT_SACOA2N,
102  GPIO_PIN_SACOA2N,
103  GPIO_FUNCTION_SACOA2N);
104  GPIO_setAsPeripheralModuleFunctionInputPin(
105  GPIO_PORT_SACOA2P,
106  GPIO_PIN_SACOA2P,
107  GPIO_FUNCTION_SACOA2P);
108 
109  //Select external source for positive input
110  //Select PGA source for negative input
111  SAC_OA_init(
112  SAC2_BASE,
113  SAC_OA_POSITIVE_INPUT_SOURCE_EXTERNAL,
114  SAC_OA_NEGATIVE_INPUT_SOURCE_PGA);
115  //Select low speed and low power mode
116  SAC_OA_selectPowerMode(
117  SAC2_BASE,
118  SAC_OA_POWER_MODE_LOW_SPEED_LOW_POWER);
119  //Set gain=3
120  SAC_PGA_setGain(
121  SAC2_BASE,
122  SAC_PGA_GAIN_BIT1);
123  //Set as non-inverting PGA mode
124  SAC_PGA_setMode(
125  SAC2_BASE,
126  SAC_PGA_MODE_NONINVERTING);
127  //Enable SAC2 OA
128  SAC_OA_enable(SAC2_BASE);
129  //Enable SAC2
130  SAC_enable(SAC2_BASE);
131 
132  //Select Pair OA source
133  SAC_OA_init(
134  SAC0_BASE,
135  SAC_OA_POSITIVE_INPUT_SOURCE_PAIR_OA,
136  SAC_OA_NEGATIVE_INPUT_SOURCE_PGA);
137  //Select low speed and low power mode
138  SAC_OA_selectPowerMode(
139  SAC0_BASE,
140  SAC_OA_POWER_MODE_LOW_SPEED_LOW_POWER);
141  //Set gain=5
142  SAC_PGA_setGain(
143  SAC0_BASE,
144  SAC_PGA_GAIN_BIT0 | SAC_PGA_GAIN_BIT1);
145  //Set as non-inverting PGA mode
146  SAC_PGA_setMode(
147  SAC0_BASE,
148  SAC_PGA_MODE_NONINVERTING);
149  //Enable SAC0 OA
150  SAC_OA_enable(SAC0_BASE);
151  //Enable SAC0
152  SAC_enable(SAC0_BASE);
153 
154  /*
155  * Configure ADC12
156  */
157  ADC_init(
158  ADC_BASE,
159  ADC_SAMPLEHOLDSOURCE_SC,
160  ADC_CLOCKSOURCE_ADCOSC,
161  ADC_CLOCKDIVIDER_1);
162  //S&H16 ADC clks
163  ADC_setupSamplingTimer(
164  ADC_BASE,
165  ADC_CYCLEHOLD_16_CYCLES,
166  ADC_MULTIPLESAMPLESDISABLE);
167  //ADCON
168  ADC_enable(ADC_BASE);
169  //12-bit conversion results
170  ADC_setResolution(
171  ADC_BASE,
172  ADC_RESOLUTION_12BIT);
173  //Enable ADC converson complete interrupt
174  ADC_enableInterrupt(
175  ADC_BASE,
176  ADC_COMPLETED_INTERRUPT);
177  //A1 ADC input select = OA0 output
178  //Vref = DVCC
179  ADC_configureMemory(
180  ADC_BASE,
181  ADC_INPUT_A1,
182  ADC_VREFPOS_AVCC,
183  ADC_VREFNEG_AVSS);
184 
185  while(1)
186  {
187  //Sampling and conversion start
188  ADC_startConversion(
189  ADC_BASE,
190  ADC_SINGLECHANNEL);
191 
192  // Enter LPM0, ADC_ISR will force exit
193  __bis_SR_register(LPM0_bits | GIE);
194  //For debug only
195  __no_operation();
196 
197  if(adcResult > 2047)
198  {
200  GPIO_PORT_LED1,
201  GPIO_PIN_LED1);
202  }
203  else
204  {
206  GPIO_PORT_LED1,
207  GPIO_PIN_LED1);
208  }
209  }
210 }
211 
212 // ADC interrupt service routine
213 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
214 #pragma vector=ADC_VECTOR
215 __interrupt void ADC_ISR(void)
216 #elif defined(__GNUC__)
217 void __attribute__ ((interrupt(ADC_VECTOR))) ADC_ISR (void)
218 #else
219 #error Compiler not supported!
220 #endif
221 {
222  switch(__even_in_range(ADCIV, ADCIV_ADCIFG))
223  {
224  case ADCIV_NONE:
225  break;
226  case ADCIV_ADCOVIFG:
227  break;
228  case ADCIV_ADCTOVIFG:
229  break;
230  case ADCIV_ADCHIIFG:
231  break;
232  case ADCIV_ADCLOIFG:
233  break;
234  case ADCIV_ADCINIFG:
235  break;
236  case ADCIV_ADCIFG:
237  //Read ADC memory
238  adcResult = ADC_getResults(ADC_BASE);
239  //Exit from LPM
240  __bic_SR_register_on_exit(LPM0_bits);
241  break;
242  default:
243  break;
244  }
245 }
246 
void ADC_ISR(void)
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
uint16_t adcResult
int main(void)